﻿using Transponder_Core.DataSource;
using Transponder_Core.DataTarget;

namespace Transponder_WebApp
{
    /// <summary>
    /// 
    /// </summary>
    public class MessageAgent
    {

        public MqttSource mMqttSource;
        /// <summary>
        /// 
        /// </summary>
        public MqttTarget mMqttTarget;

        /// <summary>
        /// 
        /// </summary>
        public string pKey = "1951111838707527681";



        public async Task  Open()
        {
            mMqttTarget = new Transponder_Core.DataTarget.MqttTarget();
            mMqttTarget.Connected += MMqttTarget_Connected;
            await mMqttTarget.Open();
             
            mMqttSource = new Transponder_Core.DataSource.MqttSource();

            mMqttSource.Connected += MMqttSource_Connected;

            mMqttSource.EventReportReceived += MMqttSource_EventReportReceived;

            mMqttSource.RealTimeElectricalParametersReceived += MMqttSource_RealTimeElectricalParametersReceived;
             
            await mMqttSource.Open();
             
        }

        private void MMqttSource_Connected(object? sender, EventArgs e)
        {
            Console.WriteLine("MMqttSource_Connected");
        }

        private void MMqttTarget_Connected(object? sender, EventArgs e)
        {
            Console.WriteLine("MMqttTarget_Connected");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pkey"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        private string SnToDeviceId(string sn)
        {
          
          return string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pkey"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        private string GetTopicByDeviceId(string pkey, string deviceId)
        {
            return "/edge/1951111838707527681/6766318567/rtg";

         
            return string.Format("/edge/{0}/{1}/rtg", pkey, deviceId);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MMqttSource_RealTimeElectricalParametersReceived(object? sender, RealTimeElectricalParametersEventArgs e)
        {
            var publishTopic = GetTopicByDeviceId("", "");
            var payload = e.Message.ToPayload();
            var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            // payload.devs
            payload.pKey = this.pKey;

            var message = payload.ToJson();

            await this.mMqttTarget.PublishAsync(publishTopic, message);
            Console.WriteLine("MMqttSource_EventReportReceived");
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private async void MMqttSource_EventReportReceived(object? sender, EventReportEventArgs e)
        {
            var publishTopic = GetTopicByDeviceId("", "");
            var payload = e.Message.ToPayload();
            var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            // payload.devs
            payload.pKey = this.pKey;

            var message = payload.ToJson();

            await this.mMqttTarget.PublishAsync(publishTopic, message);

            Console.WriteLine("MMqttSource_EventReportReceived");
        }
         
        /// <summary>
        /// 
        /// </summary>
        public MessageAgent()
        {
             
         
        }
    }
}
